home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Felix 1.1 / Loader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-16  |  3.0 KB  |  74 lines  |  [TEXT/KAHL]

  1. /*
  2.     This INIT loads the Code 128 resource and uses it to patch _FSDispatch.
  3.     Beware: _FSDispatch takes its parameters in A0 and D0. We must careful not to foul up these registers.
  4.  
  5.     The Code 128 resource must contain the string "Moof!". In its place we write the original trap address. This mechanism comes
  6.     from a snippet by Jon Wätte. [1994 Note: this is no longer the Right Way™ to do it, because writing data into code is not
  7.     clean. Today I would use Gestalt].
  8.  
  9.     What's more, our patch needs global variables (keeping their value across invocations). We create a Ptr in the System Heap for
  10.     them, and we give its address to the patch using the same [dirty] method (key string is "Data!")    
  11.  
  12.     To be cute, we display an icon at startup. Displaying an icon is something horribly complicated, so the best way is to use
  13.     a pre-existing code resource, such as ShowIcon7 by James W. Walker.    
  14.     
  15.     New in version 1.1 : a default directory can be specified using the DDIR 0 resource.
  16. */                        
  17.  
  18. #include "globals.c"
  19.  
  20. #define _FSDispatch 0xA060
  21.  
  22. main()
  23. {
  24.     char                 *moof;                                        /* Pointer used to look for Moof! */
  25.     Handle             patch;                                        /* CODE resource handle */
  26.     GlobalsPtr         Globals;                                        /* Globals memory block */
  27.     Boolean            ok;                                            /* True if things went ok */
  28.     Handle            showInit;                                        /* ShowInit7's code */
  29.     pascal void (*ShowIcon7) (short resid, Boolean adv);                    /* ShowIcon7's prototype */
  30.     Handle            ddir;
  31.         
  32.     if (!(patch = GetResource('Code', 128)))                                  /* Read in the resource */
  33.         ok = false;                                                 /* Make sure it succeeded */
  34.     else {
  35.         DetachResource(patch);                                        /* Mandatory to survive CloseResFile */ 
  36.                             
  37.         for (moof = *patch; !strcmp(moof, "Moof!"); moof++);                 /* Look for our signature */
  38.         * (long *) moof = NGetTrapAddress(_FSDispatch, OSTrap);             /* Replace it with the old trap address */
  39.         NSetTrapAddress((long) (StripAddress(*patch)), _FSDispatch, OSTrap);    /* Install our routine */
  40.                 
  41.         for (moof = *patch; !strcmp(moof, "Data!"); moof++);                /* Look for the other string */                        
  42.         if ((Globals = (GlobalsPtr) NewPtrSysClear(globalsSize)) == NULL)        /* Allocate globals */
  43.             ok = false; 
  44.         else {        
  45.             * (long *) moof =  (long) Globals;                            /* Write their address into the patch code */
  46.             ok = true;
  47.             
  48.             if (!(ddir = GetResource('DDIR', 0)))                            /* Read in the default directory */ 
  49.                 Globals->dirID = fsRtDirID;                            /* If it doesn't exist, use the root dir */
  50.             else
  51.                 Globals->dirID = * (long*) (*ddir);
  52.         }
  53.     }
  54.     
  55.         
  56.     showInit = GetResource('Code', -4048);                                /* Load ShowIcon7's code */
  57.     if (showInit != NULL) {                                            /* Make sure it succeeded */
  58.         ShowIcon7 = (pascal void (*) (short, Boolean)) StripAddress (*showInit);    
  59.         if (ok)                                                            
  60.             ShowIcon7(128, true);                                    /* Show an icon with or without a cross */
  61.         else                                                         /* depending on success */
  62.             ShowIcon7(129, true);
  63.     }
  64. }
  65.  
  66. /* String comparison routines. Have to do it by hand because we don't want to link with ANSI */
  67.  
  68. int strcmp(char * s1, char * s2) 
  69. {
  70.     while (*s1 == *s2 && *s2) {
  71.         s1++; s2++;
  72.     }
  73.     return (*s1 == *s2);
  74. }